package com.book.bigwork.Webcontroller;

import com.book.bigwork.Webservice.BookService;
import com.book.bigwork.model.Book;
import com.book.bigwork.model.BookForm;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.annotation.Secured;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@Controller
public class BookController {
    private final BookService bookService;
    @Autowired
    public BookController(BookService bookService) {
        this.bookService = bookService;
    }
    @GetMapping(value = {"/home", "/"})
    public String listAllBooks(Model model) {
        List<Book> books = bookService.findAllBooks();
        model.addAttribute("books", books);
        return "home";
    }
    @GetMapping("/search")
    public String searchBooks(@RequestParam(name="q", defaultValue="") String
                                      query,Model model) {
        List<Book> books = bookService.searchBooks(query);
        model.addAttribute("books", books);
        return "search";
    }
    @GetMapping("/bookImage")
    public void bookImage(HttpServletResponse response,
                          @RequestParam("bookId") Long bookId) throws
            IOException {
        Book book = bookService.findBookById(bookId);
        if (book != null) {
            response.setContentType("image/jpg");
            response.getOutputStream().write(book.getBookImage());
        }
        response.getOutputStream().close();
    }
    @GetMapping("/books/{bookId:\\d+}")
    public String getBook(@PathVariable Long bookId, Model model) throws
            Exception{
        Book book = bookService.findBookById(bookId);
        try{
            model.addAttribute("book", book);
        }catch(Exception ex){
            throw new Exception("找不到图书");
        }
        return "book";
    }
    @GetMapping("books")
    @Secured("ROLE_ADMIN")
    public String getBooks(Model model) {
        List<Book> books = bookService.findAllBooks();
        model.addAttribute("books", books);
        return "showAllBooks";
    }
    @GetMapping("/books/add")
    @Secured ("ROLE_ADMIN")
    public String addBookPage(Model model) {
        BookForm bookForm = new BookForm();
        model.addAttribute("bookForm", bookForm);
        return "addBook";
    }
    @PostMapping("/books/add")
    @Secured ("ROLE_ADMIN")
    public String addBook(@ModelAttribute BookForm bookForm, Model model) {
        Book savedBook = bookService.saveBook(bookForm);
        model.addAttribute("successMessage", "图书创建编号 " + savedBook.
                getBookId());
        return addBookPage(model);
    }
    @GetMapping("/books/edit/{bookId}")
    @Secured ("ROLE_ADMIN")
    public String editBookPage(@PathVariable Long bookId, Model model) {
        Book book = bookService.findBookById(bookId);
        model.addAttribute("bookForm", new BookForm(book));
        return "editBook";
    }
    @PostMapping("/books/edit")
    @Secured ("ROLE_ADMIN")
    public String editBook(@ModelAttribute BookForm bookForm, Model model) {
        Book savedBook = bookService.saveBook(bookForm);
        model.addAttribute("successMessage", "图书信息编辑成功 " + savedBook.
                getBookId());
        return "editBook";
    }
    @GetMapping("/books/delete/{bookId}")
    @Secured ("ROLE_ADMIN")
    public String deleteBook(@PathVariable Long bookId, Model model) {
        Book book = bookService.findBookById(bookId);
        bookService.deleteBook(book);
        model.addAttribute("successMessage", "图书编号"+ bookId + " 删除成功");
        return getBooks(model);
    }
    @GetMapping("/books/view/{bookId}")
    @Secured ("ROLE_ADMIN")
    public String viewBook(@PathVariable Long bookId, Model model) {
        Book book = bookService.findBookById(bookId);
        model.addAttribute("book", book);
        return "viewBook";
    }
}
